﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using KOPLibrary.TemplateManagement;
using KOPLibrary.Exceptions;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Reflection;

namespace KOPLibrary.PreferenceManagement
{
    public static class PreferenceManager
    {
        private static string RootKey = @"Software\Kalitte\KOP";

        public static List<string> GetGroupValues(string groupName)
        {
            try
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey(RootKey + "\\" + groupName);
                if (key == null)
                    return new List<string>();
                List<string> result = new List<string>();
                foreach (var vName in key.GetValueNames())
                    result.Add((string)key.GetValue(vName));
                key.Close();
                return result;
            }
            catch (Exception ee)
            {
                ExceptionManager.ShowException(ee);
                return null;
            }
        }

        public static void SaveGroupValues(string groupName, Dictionary<string, string> values)
        {
            try
            {
                RegistryKey key = Registry.CurrentUser.CreateSubKey(RootKey + "\\" + groupName, RegistryKeyPermissionCheck.ReadWriteSubTree);
                string[] valueNames = key.GetValueNames();
                foreach (var vName in valueNames)
                    key.DeleteValue(vName, false);
                foreach (KeyValuePair<string, string> item in values)
                    key.SetValue(item.Key, item.Value, RegistryValueKind.String);
                key.Close();
            }
            catch (Exception ee)
            { ExceptionManager.ShowException(ee); }
        }

        public static void SaveKeyToTypeRoot(string keyname, string keyvalue, TemplateType type)
        {
            try
            {
                RegistryKey key = Registry.CurrentUser.CreateSubKey(RootKey + "\\" + type.ToString(), RegistryKeyPermissionCheck.ReadWriteSubTree);
                key.SetValue(keyname, keyvalue, RegistryValueKind.String);
                key.Close();
            }
            catch (Exception ex)
            {
                ExceptionManager.ShowException(ex);
            }
        }
        public static void SaveKey(string keyname, string keyvalue)
        {
            try
            {
                RegistryKey key = Registry.CurrentUser.CreateSubKey(RootKey, RegistryKeyPermissionCheck.ReadWriteSubTree);
                key.SetValue(keyname, keyvalue, RegistryValueKind.String);
                key.Close();
            }
            catch (Exception ex)
            {
                ExceptionManager.ShowException(ex);
            }
        }
        public static string ReadKey(string keyname, TemplateType type)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(RootKey + "\\" + type.ToString());
            try
            {
                return key.GetValue(keyname).ToString();
            }
            catch
            {
                return string.Empty;
            }
        }

        public static string ReadKey(string keyname)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(RootKey);
            try
            {
                return key.GetValue(keyname, "").ToString();
            }
            catch
            {
                return string.Empty;
            }
        }

        public static object ReadObject(string keyname, TemplateType type)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(RootKey + "\\" + type.ToString());
            try
            {
                byte [] value = (byte [])key.GetValue(keyname, null);
                BinaryFormatter formatter = new BinaryFormatter();
                MemoryStream stream = new MemoryStream(value);
                object o = formatter.Deserialize(stream);
                stream.Close();
                return o;
            }
            catch
            {
                return null;
            }
        }

        public static void SaveObject(string keyname, TemplateType type, object instance)
        {
            RegistryKey key = Registry.CurrentUser.CreateSubKey(RootKey + "\\" + type.ToString(), RegistryKeyPermissionCheck.ReadWriteSubTree);
            try
            {                
                BinaryFormatter formatter = new BinaryFormatter();
                MemoryStream stream = new MemoryStream();
                formatter.Serialize(stream, instance);
                stream.Seek(0, SeekOrigin.Begin);
                byte[] buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);
                stream.Close();
                key.SetValue(keyname, buffer, RegistryValueKind.Binary);
            }
            catch
            {
                
            }
        }

        public static string GetDefaulTemplatePath(TemplateType type)
        {
            string rootpath;
            rootpath = PreferenceManager.ReadKey("DefaultTemplatePath", type);
            if (string.IsNullOrEmpty(rootpath))
            {
                try
                {
                    rootpath = Path.Combine(PreferenceManager.ReadKey("InstallPath"), @"Kurumsal Ofis Şablonları\" + type.ToString());
                }
                catch
                {
                    rootpath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                }
                PreferenceManager.SaveKeyToTypeRoot("DefaultTemplatePath", rootpath, type);
            }
            return rootpath;
        }

    }
}
